﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.DAO;

namespace QuanLyKhachSan
{
    public partial class frmCapNhatThongTinTaiKhoan : Form
    {
        public frmCapNhatThongTinTaiKhoan()
        {
            InitializeComponent();
        }
        private NguoiDungDTO nd ;//= (NguoiDungDTO)this.Tag;

        public NguoiDungDTO ND
        {
            get { return nd; }
            set { nd = value; }
        }

        private void HienThongTinNguoiDung()
        {
            nd = (NguoiDungDTO)this.Tag;

            txtTenDangNhap.Text = nd.TenDangNhap;
            txtTenND.Text = nd.TenNguoiDung;
            txtTenDemND.Text = nd.TenLot;
            txtHoND.Text = nd.HoNguoiDung;
            txtDiaChi.Text = nd.DiaChi;
            txtCMND.Text = nd.CMND1;
        }
        private void LayThongTinNguoiDung()
        {
            nd.TenNguoiDung = txtTenND.Text;
            nd.HoNguoiDung = txtHoND.Text;
            if (string.IsNullOrEmpty(txtMatKhauMoi.Text) == false)
                nd.MatKhau = NguoiDungDTO.MaHoaMD5( txtMatKhauMoi.Text);
            nd.TenLot = txtTenDemND.Text;
            nd.CMND1 = txtCMND.Text;
            nd.DiaChi = txtDiaChi.Text;
        }
        private void frmCapNhatThongTinTaiKhoan_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;

            HienThongTinNguoiDung();

            toolTipMatKhauMoi.ShowAlways = true;
            toolTipMatKhauMoi.ToolTipTitle = "Tooltip Mật Khẩu Mới";
            toolTipMatKhauMoi.UseAnimation = true;
            toolTipMatKhauMoi.SetToolTip(this.txtMatKhauMoi, "Nếu Không muốn đổi mật khẩu thì bạn hãy nhập lại mật khẩu hiện tại của bạn");
            
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Ban co chac chan muon thoat?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (kq == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            DialogResult dlr = MessageBox.Show("Ban Chac Chan Muon Huy Thong Tin Da Nhap?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlr == DialogResult.Yes)
            {
                HienThongTinNguoiDung();
                txtMatKhauCu.Text = "";
                txtMatKhauMoi.Text = "";
            }
        }
        private bool KiemTraNhapLieu()
        {
            if (string.Equals(NguoiDungDTO.MaHoaMD5(txtMatKhauCu.Text), nd.MatKhau) == false)
            {
                MessageBox.Show("Mat Khau Cu Cua Ban Khong Dung!");
                txtMatKhauCu.Text = "";
                txtMatKhauCu.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtMatKhauMoi.Text) == true)
            {
                MessageBox.Show("Hay Nhap Mat Khau Moi\nNeu Khong Muon Doi Thi Nhap Lai Mat Khau Hien Tai Cua Ban ");
                txtMatKhauMoi.Text = "";
                txtMatKhauMoi.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtTenND.Text) == true)
            {
                MessageBox.Show("Ten Khong Duoc Bo Trong");
                txtTenND.Text = nd.TenNguoiDung;
                txtTenND.Focus();
                return false;
            }
            if (NguoiDungDTO.KiemTraHoTen(txtTenND.Text) == false)
            {
                MessageBox.Show("Ten Ban Khong Hop Le!");
                txtTenND.Text = nd.TenNguoiDung;
                txtTenND.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtHoND.Text) == true)
            {
                MessageBox.Show("Ho cua Ban Khong Duoc Bo Trong");
                txtHoND.Text = nd.HoNguoiDung;
                txtHoND.Focus();
                return false;
            }
            if (NguoiDungDTO.KiemTraHoTen(txtHoND.Text) == false)
            {
                MessageBox.Show("Ho cua Ban Khong Hop Le!");
                txtHoND.Text = nd.HoNguoiDung;
                txtHoND.Focus();
                return false;
            }
            if (NguoiDungDTO.KiemTraHoTen(txtTenDemND.Text) == false)
            {
                MessageBox.Show("Ten Dem cua Ban Khong Hop Le!");
                txtTenDemND.Text = nd.TenLot;
                txtTenDemND.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCMND.Text) == true)
            {
                MessageBox.Show("CMND Khong Duoc Trong!");
                txtCMND.Text = nd.CMND1;
                txtCMND.Focus();
                return false;
            }
            if (NguoiDungDTO.KiemTraCMND(txtCMND.Text) == false)
            {
                MessageBox.Show("CMND Khong Hop Le!");
                txtCMND.Text = nd.CMND1;
                txtCMND.Focus();
                return false;
            }
            if (NguoiDungDTO.KiemTraDiaChi(txtDiaChi.Text) == false)
            {
                MessageBox.Show("Dia Chi cua Ban Khong Hop Le!");
                txtDiaChi.Text = nd.DiaChi;
                txtDiaChi.Focus();
                return false;
            }
            return true;
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (KiemTraNhapLieu())
            {
                DialogResult dlr = MessageBox.Show("Ban Chac Chan Muon Cap Nhat Thong Tin?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlr == DialogResult.Yes)
                {
                    LayThongTinNguoiDung();
                    bool kq =NguoiDungDAO.Update(nd);
                    if (kq)
                    {
                        MessageBox.Show("Cap Nhat Thong Tin Thanh Cong");
                    } 
                }
            }
        }
        
    }
}